import Vue from 'vue';
import Vuex from 'vuex';

import { setStore, getStore } from '@/utils/util';
import { ADMINUSERID, ADMINACCOUNT, AVATAR, TOKEN, REMEMBER, PASSWORD } from '@/utils/const';

import routerLink from './modules/router-link'

Vue.use(Vuex);

const state = {
  adminUserId: getStore(ADMINUSERID) || "",
  adminAccount: getStore(ADMINACCOUNT) || "",
  password: getStore(PASSWORD) || "",
  avatar: getStore(AVATAR) || "",
  remember: getStore(REMEMBER) ? JSON.parse(getStore(REMEMBER)) : false,
  token: getStore(TOKEN)
};

const mutations = {
  // 更新本地缓存中用户数据
  UPDATE_USER(state, info) {
    let { adminUserId, adminAccount, password, avatar, remember, token } = info;
    state.adminUserId = adminUserId;
    state.adminAccount = adminAccount;
    state.password = password;
    state.avatar = avatar;
    state.remember = remember;
    state.token = token;
    setStore(ADMINUSERID, state.adminUserId);
    setStore(ADMINACCOUNT, state.adminAccount);
    setStore(PASSWORD, state.password);
    setStore(AVATAR, state.avatar);
    setStore(REMEMBER, state.remember);
    setStore(TOKEN, state.token);
  },
};

// 登录验证
export default new Vuex.Store({
  state,
  mutations,
  // 对应模块处理
  modules: {
    routerLink
  }
})